import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class PageViewPage3 extends StatefulWidget {
  const PageViewPage3({super.key});
  @override
  State<PageViewPage3> createState() => _PageViewPageState();
}
class _PageViewPageState extends State<PageViewPage3> {

  final List<Widget> _list = [];

  @override
  void initState() {
    super.initState();
    for (var i = 0; i < 10; i++) {
      _list.add(MyPage(text: "$i"));
    }
  }

  @override
    Widget build(BuildContext context) {
      return Scaffold(
          appBar: AppBar(
            title: const Text("pageview演示"),
          ),
          body: PageView(
            scrollDirection: Axis.vertical, // 滑动方向为垂直方向
            onPageChanged: (index) {
              print('onPageChanged 触发了  $index   集合大小：${_list.length}');
             // print();
              if (index + 2 == _list.length) {
                setState(() {
                  var tempNumer=_list.length+10;
                  var indexNow=_list.length;
                  for (var i = indexNow; i < tempNumer; i++) {
                    _list.add(MyPage(text: "$i"));
                  }
                });
              }
            },
            children: _list,
          ));
    }

}

class MyPage extends StatefulWidget {
  final String text;
  const MyPage({super.key,required this.text});
  @override
  State<MyPage> createState() => _MyPageState();
}
class _MyPageState extends State<MyPage> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text(widget.text,style: Theme.of(context).textTheme.headline1),
    );
  }
}

